using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[Serializable]
public class preimage_edit_class
{
	public List<image_edit_class> edit_color;

	public int y1;

	public int x1;

	public int x;

	public int y;

	public float frames;

	public float auto_speed_time;

	private float target_frame;

	public float time_start;

	public float time;

	public bool generate;

	public bool loop;

	public bool generate_call;

	public bool active;

	public bool loop_active;

	public bool import_settings;

	public bool regen;

	public bool regenRaw;

	public bool border;

	public float progress;

	public Vector2 resolution;

	public Vector2 resolutionRaw;

	public byte[] byte1;

	public bool raw;

	public int xx;

	public Vector2 position;

	public Vector2 position2;

	public Vector2 direction;

	public int dir;

	public Vector2 pos_old;

	public bool first;

	public int count;

	public buffer_class inputBuffer;

	public buffer_class outputBuffer;

	public int radius;

	public int radiusSelect;

	public int mode;

	public tile_class tile;

	public int repeat;

	public int repeatAmount;

	public bool content;

	public preimage_edit_class()
	{
		edit_color = new List<image_edit_class>();
		target_frame = 30f;
		active = true;
		loop_active = true;
		byte1 = new byte[3];
		xx = 3;
		position = new Vector2(x - 1, y - 1);
		direction = new Vector2(1f, 0f);
		dir = 1;
		inputBuffer = new buffer_class();
		outputBuffer = new buffer_class();
		radius = 300;
		radiusSelect = 300;
		mode = 1;
		tile = new tile_class();
		repeatAmount = 3;
		content = true;
	}

	public float calc_color_pos(Color color, Color color_start, Color color_end)
	{
		Color color2 = color_start;
		Color color3 = default(Color);
		if (!(color_start.r <= color_end.r))
		{
			color_start.r = color_end.r;
			color_end.r = color2.r;
		}
		if (!(color_start.g <= color_end.g))
		{
			color_start.g = color_end.g;
			color_end.g = color2.g;
		}
		if (!(color_start.b <= color_end.b))
		{
			color_start.b = color_end.b;
			color_end.b = color2.b;
		}
		color3 = color_end - color_start;
		color -= color_start;
		float result;
		if (color.r < 0f || color.g < 0f || !(color.b >= 0f))
		{
			result = -1f;
		}
		else if (color.r > color3.r || color.g > color3.g || !(color.b <= color3.b))
		{
			result = -1f;
		}
		else
		{
			float num = color3.r + color3.g + color3.b;
			float num2 = color.r + color.g + color.b;
			result = ((num == 0f) ? 1f : (num2 / num));
		}
		return result;
	}

	public Color calc_color_from_pos(float pos, Color color_start, Color color_end)
	{
		Color color = color_start;
		Color color2 = default(Color);
		if (!(color_start.r <= color_end.r))
		{
			color_start.r = color_end.r;
			color_end.r = color.r;
		}
		if (!(color_start.g <= color_end.g))
		{
			color_start.g = color_end.g;
			color_end.g = color.g;
		}
		if (!(color_start.b <= color_end.b))
		{
			color_start.b = color_end.b;
			color_end.b = color.b;
		}
		color2 = color_end - color_start;
		return color_start + new Color(color2.r * pos, color2.g * pos, color2.b * pos);
	}

	public void swap_color(int color_index1, int color_index2)
	{
		image_edit_class value = edit_color[color_index1];
		edit_color[color_index1] = edit_color[color_index2];
		edit_color[color_index2] = value;
	}

	public void copy_color(int color_index1, int color_index2)
	{
		edit_color[color_index1].color1_start = edit_color[color_index2].color1_start;
		edit_color[color_index1].color1_end = edit_color[color_index2].color1_end;
		edit_color[color_index1].curve1 = edit_color[color_index2].curve1;
		edit_color[color_index1].color2_start = edit_color[color_index2].color2_start;
		edit_color[color_index1].color2_end = edit_color[color_index2].color2_end;
		edit_color[color_index1].curve2 = edit_color[color_index2].curve2;
		edit_color[color_index1].strength = edit_color[color_index2].strength;
		edit_color[color_index1].output = edit_color[color_index2].output;
		edit_color[color_index1].active = edit_color[color_index2].active;
		edit_color[color_index1].solid_color = edit_color[color_index2].solid_color;
	}

	public void convert_texture_raw(bool multithread)
	{
		int num = 0;
		Color color = default(Color);
		Color color2 = default(Color);
		Color color3 = default(Color);
		float num2 = default(float);
		float num3 = default(float);
		float num4 = default(float);
		float num5 = default(float);
		bool flag = true;
		auto_speed_time = Time.realtimeSinceStartup;
		pos_old.y = -100f;
		y = y1;
		while ((float)y < inputBuffer.innerRect.height + inputBuffer.offset.y)
		{
			xx = 3;
			position = new Vector2(-1f, y - 1);
			direction = new Vector2(1f, 0f);
			dir = 1;
			count = 0;
			x = (int)(inputBuffer.offset.x + (float)x1);
			while ((float)x < inputBuffer.innerRect.width + inputBuffer.offset.x)
			{
				color = GetPixelRaw(inputBuffer, x, y);
				color3 = color;
				for (num = 0; num < edit_color.Count; num++)
				{
					if (!edit_color[num].active && !edit_color[num].solid_color)
					{
						continue;
					}
					num2 = calc_color_pos(color, edit_color[num].color1_start, edit_color[num].color1_end);
					if (num2 == -1f)
					{
						continue;
					}
					num2 = edit_color[num].curve1.Evaluate(num2);
					num3 = edit_color[num].curve2.Evaluate(num2);
					color2 = calc_color_from_pos(num3, edit_color[num].color2_start, edit_color[num].color2_end);
					num4 = edit_color[num].strength;
					if (!edit_color[num].solid_color)
					{
						if (edit_color[num].output == image_output_enum.content)
						{
							if ((float)x == pos_old.x + 1f && (float)y == pos_old.y && xx > 3)
							{
								if (dir == 1)
								{
									if (count == 0)
									{
										position.x += 1f;
										xx -= 2;
									}
									else
									{
										count--;
									}
								}
								else if (dir == 2)
								{
									xx -= 2;
								}
								else if (dir == 3 || dir == 4)
								{
									position.x = x + (xx - 1) / 2;
									position.y = y - (xx - 1) / 2;
									dir = 2;
									count = 0;
									direction = new Vector2(-1f, 0f);
								}
								color2 = content_fill_raw(x, y, edit_color[num].color1_start, edit_color[num].color1_end, edit_color[num].color2_start, reset: false);
							}
							else
							{
								color2 = content_fill_raw(x, y, edit_color[num].color1_start, edit_color[num].color1_end, edit_color[num].color2_start, reset: true);
							}
							pos_old = new Vector2(x, y);
							color3 = color2;
						}
						switch (edit_color[num].output)
						{
						case image_output_enum.add:
							color3.r += color2.r * num4;
							color3.g += color2.g * num4;
							color3.b += color2.b * num4;
							break;
						case image_output_enum.subtract:
							color3.r -= color2.r * num4;
							color3.g -= color2.g * num4;
							color3.b -= color2.b * num4;
							break;
						case image_output_enum.change:
							color3.r = color.r * (1f - num4) + color2.r * num4;
							color3.g = color.g * (1f - num4) + color2.g * num4;
							color3.b = color.b * (1f - num4) + color2.b * num4;
							break;
						case image_output_enum.multiply:
							color3.r *= color2.r * num4;
							color3.g *= color2.g * num4;
							color3.b *= color2.b * num4;
							break;
						case image_output_enum.divide:
							if (color2.r * num4 != 0f)
							{
								color3.r = color.r / (color2.r * num4);
							}
							if (color2.g * num4 != 0f)
							{
								color3.g = color.g / (color2.g * num4);
							}
							if (color2.b * num4 != 0f)
							{
								color3.b = color.b / (color2.b * num4);
							}
							break;
						case image_output_enum.difference:
							color3.r = Mathf.Abs(color2.r * num4 - color.r);
							color3.g = Mathf.Abs(color2.g * num4 - color.g);
							color3.b = Mathf.Abs(color2.b * num4 - color.b);
							break;
						case image_output_enum.average:
							color3.r = (color.r + color2.r * num4) / 2f;
							color3.g = (color.g + color2.g * num4) / 2f;
							color3.b = (color.b + color2.b * num4) / 2f;
							break;
						case image_output_enum.max:
							if (!(color2.r * num4 <= color.r))
							{
								color3.r = color2.r * num4;
							}
							if (!(color2.g * num4 <= color.g))
							{
								color3.g = color2.g * num4;
							}
							if (!(color2.b * num4 <= color.b))
							{
								color3.b = color2.b * num4;
							}
							break;
						case image_output_enum.min:
							if (!(color2.r * num4 >= color.r))
							{
								color3.r = color2.r * num4;
							}
							if (!(color2.g * num4 >= color.g))
							{
								color3.g = color2.g * num4;
							}
							if (!(color2.b * num4 >= color.b))
							{
								color3.b = color2.b * num4;
							}
							break;
						}
					}
					else
					{
						color3.r += 1f - num2;
						color3.g += num2;
						color3.b += 1f;
					}
				}
				if (!(color3[0] <= 1f))
				{
					color3[0] = 1f;
				}
				else if (!(color3[0] >= 0f))
				{
					color3[0] = 0f;
				}
				if (!(color3[1] <= 1f))
				{
					color3[1] = 1f;
				}
				else if (!(color3[1] >= 0f))
				{
					color3[1] = 0f;
				}
				if (!(color3[2] <= 1f))
				{
					color3[2] = 1f;
				}
				else if (!(color3[2] >= 0f))
				{
					color3[2] = 0f;
				}
				SetPixelRaw(outputBuffer, x, y, color3);
				if (!(Time.realtimeSinceStartup - auto_speed_time <= 1f / target_frame) && multithread)
				{
					y1 = y;
					x1 = (int)((float)x - inputBuffer.offset.x + 1f);
					if (mode == 2)
					{
						time = Time.realtimeSinceStartup - time_start;
					}
					return;
				}
				x++;
			}
			x1 = 0;
			y++;
		}
		generate = false;
	}

	public Color content_fill_raw(int _x, int _y, Color exclude_start, Color exclude_end, Color exclude2, bool reset)
	{
		Vector2 vector = default(Vector2);
		Vector2 vector2 = default(Vector2);
		Color color = default(Color);
		Color color2 = default(Color);
		Color color3 = default(Color);
		float num = 0f;
		float num2 = 360f;
		float num3 = 20f;
		Vector2 a = default(Vector2);
		Vector2 vector3 = default(Vector2);
		float num4 = default(float);
		float num5 = default(float);
		bool flag = false;
		bool flag2 = false;
		float num6 = default(float);
		float num7 = default(float);
		bool flag3 = false;
		bool flag4 = false;
		if (reset)
		{
			xx = 3;
			position = new Vector2(_x - 1, _y - 1);
			direction = new Vector2(1f, 0f);
			dir = 1;
			count = 0;
		}
		do
		{
			IL_00bb:
			color = GetPixelRaw(inputBuffer, (long)position.x, (long)position.y);
			if (!color_in_range(exclude_start, exclude_end, color))
			{
				flag = true;
				break;
			}
			count++;
			if (count >= xx && dir == 1)
			{
				direction = new Vector2(0f, 1f);
				count = 0;
				dir = 2;
			}
			else if (count >= xx - 1 && dir == 2)
			{
				direction = new Vector2(-1f, 0f);
				count = 0;
				dir = 3;
			}
			else if (count >= xx - 1 && dir == 3)
			{
				direction = new Vector2(0f, -1f);
				count = 0;
				dir = 4;
			}
			else if (count >= xx - 2 && dir == 4)
			{
				direction = new Vector2(1f, 0f);
				count = 0;
				position += new Vector2(-1f, -2f);
				dir = 1;
				xx += 2;
				goto IL_00bb;
			}
			position += direction;
		}
		while (!flag);
		a.x = position.x - (float)_x;
		a.y = position.y - (float)_y;
		num4 = a.magnitude;
		if (repeat < 1 && !(num4 <= 4f))
		{
			for (int i = (int)(position.y - 1f); (float)i <= position.y + 1f; i++)
			{
				for (int j = (int)(position.x - 1f); (float)j <= position.x + 1f; j++)
				{
					color2 = GetPixelRaw(inputBuffer, j, i);
					if (!(color2[0] > exclude2[0]) && !(color2[1] > exclude2[1]) && !(color2[2] > exclude2[2]))
					{
						SetPixelRaw(outputBuffer, j, i, new Color(0f, 0f, 0f));
					}
				}
			}
		}
		if (repeat < repeatAmount - 1)
		{
			vector3 = a / num4 * radius;
			position2.x = (float)_x + vector3.x;
			position2.y = (float)_y + vector3.y;
			color = GetPixelRaw(inputBuffer, (long)position2.x, (long)position2.y);
			if (color_in_range(exclude_start, exclude_end, color))
			{
				regen = true;
				flag4 = true;
			}
		}
		if (!flag4)
		{
			color2 = GetPixelRaw(outputBuffer, _x - 1, _y);
			if (!color_in_range(exclude_start, exclude_end, color2))
			{
				num5 = color_difference(color2, color);
				if (GetPixelRaw(inputBuffer, _x - 1, _y) == color2)
				{
					num5 *= 10f;
				}
				if (!(num5 <= num3))
				{
					color3 += color2 * (num5 / num2);
					num += num5 / num2;
				}
			}
			color2 = GetPixelRaw(outputBuffer, _x, _y - 1);
			if (!color_in_range(exclude_start, exclude_end, color2))
			{
				num5 = color_difference(color2, color);
				if (GetPixelRaw(inputBuffer, _x, _y - 1) == color2)
				{
					num5 *= 10f;
				}
				if (!(num5 <= num3))
				{
					color3 += color2 * (num5 / num2);
					num += num5 / num2;
				}
			}
			color2 = GetPixelRaw(outputBuffer, _x + 1, _y);
			if (!color_in_range(exclude_start, exclude_end, color2))
			{
				num5 = color_difference(color2, color);
				if (GetPixelRaw(inputBuffer, _x + 1, _y) == color2)
				{
					num5 *= 10f;
				}
				if (!(num5 <= num3))
				{
					color3 += color2 * (num5 / num2);
					num += num5 / num2;
				}
			}
			color2 = GetPixelRaw(outputBuffer, _x, _y + 1);
			if (!color_in_range(exclude_start, exclude_end, color2))
			{
				num5 = color_difference(color2, color);
				if (GetPixelRaw(inputBuffer, _x, _y + 1) == color2)
				{
					num5 *= 10f;
				}
				if (!(num5 <= num3))
				{
					color3 += color2 * (num5 / num2);
					num += num5 / num2;
				}
			}
			color += color3;
			color /= 1f + num;
		}
		SetPixelRaw(outputBuffer, _x, _y, color);
		return color;
	}

	public Color GetPixelRaw(buffer_class buffer, long x, long y)
	{
		Color result;
		if (mode == 1)
		{
			if (x < 0)
			{
				x = -x;
			}
			else if (!((float)x <= buffer.outerRect.width - 1f))
			{
				x = (long)((float)x - ((float)x - (buffer.outerRect.width - 1f)));
			}
			if (y < 0)
			{
				y = -y;
			}
			else if (!((float)y <= buffer.outerRect.height - 1f))
			{
				y = (long)((float)y - ((float)y - (buffer.outerRect.height - 1f)));
			}
		}
		else if (x < 0 || (float)x > buffer.outerRect.width - 1f || y < 0 || !((float)y <= buffer.outerRect.height - 1f))
		{
			result = GetPixelRaw2(buffer, x, y);
			goto IL_0154;
		}
		ulong num = (ulong)(buffer.outerRect.width * 3f * (float)y + (float)(x * 3));
		result = new Color((float)(int)buffer.bytes[(uint)num] * 1f / 255f, (float)(int)buffer.bytes[(uint)(num + 1u)] * 1f / 255f, (float)(int)buffer.bytes[(uint)(num + 2u)] * 1f / 255f);
		goto IL_0154;
		IL_0154:
		return result;
	}

	public void SetPixelRaw(buffer_class buffer, long x, long y, Color color)
	{
		if (x < 0)
		{
			x = -x;
		}
		else if (!((float)x <= buffer.outerRect.width - 1f))
		{
			x = (long)((float)x - ((float)x - (buffer.outerRect.width - 1f)));
		}
		if (y < 0)
		{
			y = -y;
		}
		else if (!((float)y <= buffer.outerRect.height - 1f))
		{
			y = (long)((float)y - ((float)y - (buffer.outerRect.height - 1f)));
		}
		ulong num = (ulong)(buffer.outerRect.width * 3f * (float)y + (float)(x * 3));
		buffer.bytes[(uint)num] = (byte)(color[0] * 255f);
		buffer.bytes[(uint)(num + 1u)] = (byte)(color[1] * 255f);
		buffer.bytes[(uint)(num + 2u)] = (byte)(color[2] * 255f);
	}

	public Color GetPixelRaw2(buffer_class buffer, long x, long y)
	{
		x = (long)((float)x + buffer.outerRect.x);
		y = (long)((float)y + buffer.outerRect.y);
		if (x < 0)
		{
			x = -x;
		}
		else if (!((float)x <= buffer.resolution.x - 1f))
		{
			x = (long)((float)x - ((float)x - buffer.resolution.x - 1f));
		}
		if (y < 0)
		{
			y = -y;
		}
		else if (!((float)y <= buffer.resolution.y - 1f))
		{
			y = (long)((float)y - ((float)y - buffer.resolution.y - 1f));
		}
		ulong num = (ulong)((long)buffer.row * y + x * 3);
		buffer.file.Seek((long)num, SeekOrigin.Begin);
		byte[] array = new byte[3];
		buffer.file.Read(array, 0, 3);
		return new Color((float)(int)array[0] * 1f / 255f, (float)(int)array[1] * 1f / 255f, (float)(int)array[2] * 1f / 255f);
	}

	public bool color_in_range(Color color_start, Color color_end, Color color)
	{
		return (!(color[0] < color_start[0]) && !(color[0] > color_end[0]) && !(color[1] < color_start[1]) && !(color[1] > color_end[1]) && !(color[2] < color_start[2]) && !(color[2] > color_end[2])) ? true : false;
	}

	public int color_difference(Color color1, Color color2)
	{
		return (int)((Mathf.Abs(color1[0] - color2[0]) + Mathf.Abs(color1[1] - color2[1]) + Mathf.Abs(color1[2] - color2[1])) * 255f);
	}
}
